package com.shujia.mr.kqzl2;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

// reduce输入<"1001",["$20180603-50","#万寿福宫，北京"]>
//reduce输出<"20180603-万寿福宫，北京","50">
public class PM25JoinCityReduce extends Reducer<Text,Text,Text,Text> {
    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
       String city = "未知";
       String date = null;
       String avg_pm25 = null;
        for (Text value : values) {
            String line = value.toString();
            if (line.startsWith("&")) {
                String[] infos = line.substring(1).split("-");
                date = infos[0];
                avg_pm25 = infos[1];
            }else if (line.startsWith("#")) {
                 city = line.substring(1);

            }


        }
        context.write(new Text(date+"-"+city),new Text(avg_pm25));
    }
}
